﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Pager.Entity;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmDictionary : BaseForm
    {
        public FrmDictionary()
        {
            InitializeComponent();
        }

        private void FrmDictionary_Load(object sender, EventArgs e)
        {
            InitTree();
            this.lblDictType.Text = "";

            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
            this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip2;
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    BLLFactory<DictData>.Instance.Delete(row.Cells["ID"].Value.ToString());
                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    FrmEditDictData dlg = new FrmEditDictData();
                    dlg.ID = row.Cells["ID"].Value.ToString();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        BindData();
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            if (this.lblDictType.Text.Length == 0)
            {
                MessageUtil.ShowTips("请选择指定的字典大类，然后添加！");
                return;
            }

            FrmEditDictData dlg = new FrmEditDictData();
            dlg.txtDictType.Text = this.lblDictType.Text;
            dlg.txtDictType.Tag = this.lblDictType.Tag;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<DictData>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.DisplayColumns = "ID,Name,Value,Seq,Remark,EditTime";
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("DictType_ID", "字典大类");
            this.winGridViewPager1.AddColumnAlias("Name", "项目名称");
            this.winGridViewPager1.AddColumnAlias("Value", "项目值");
            this.winGridViewPager1.AddColumnAlias("Seq", "字典排序");
            this.winGridViewPager1.AddColumnAlias("Remark", "备注");
            this.winGridViewPager1.AddColumnAlias("Editor", "修改用户");
            this.winGridViewPager1.AddColumnAlias("EditTime", "更新日期");
            #endregion

            if (this.lblDictType.Tag != null)
            {
                SearchCondition conditon = new SearchCondition();
                conditon.AddCondition("DictType_ID", this.lblDictType.Tag.ToString(), SqlOperator.Equal);
                string sql = conditon.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
                List<DictDataInfo> list = BLLFactory<DictData>.Instance.Find(sql, this.winGridViewPager1.PagerInfo);
                this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<DictDataInfo>(list);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void InitTree()
        {
            this.treeView1.BeginUpdate();
            for (int i = 0; i < 3; i++)
            {
                TreeNode rootNode = this.treeView1.Nodes[i];
                rootNode.Nodes.Clear();

                Dictionary<string, string> dictTypeList = BLLFactory<DictType>.Instance.GetAllType(i);
                TreeNode node = null;
                foreach (string key in dictTypeList.Keys)
                {
                    node = new TreeNode(key, 1, 1);
                    node.Tag = dictTypeList[key];
                    rootNode.Nodes.Add(node);
                }
            }
            this.treeView1.EndUpdate();
            this.treeView1.Nodes[0].ExpandAll();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                this.lblDictType.Text = e.Node.Text;
                this.lblDictType.Tag = e.Node.Tag;

                BindData();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnAddNew(this.winGridViewPager1.dataGridView1, null);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnEditSelected(this.winGridViewPager1.dataGridView1, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnDeleteSelected(this.winGridViewPager1.dataGridView1, null);
        }
                
        private int GetParentNodeIndex()
        {
            TreeNode node = this.treeView1.SelectedNode;
            if (node != null && node.Parent != null)
            {
                node = node.Parent;
            }
            return node.Index;
        }

        private void menu_AddType_Click(object sender, EventArgs e)
        {
            FrmEditDictType dlg = new FrmEditDictType();
            dlg.category = GetParentNodeIndex();
            dlg.ShowDialog();
        }

        private void menu_DeleteType_Click(object sender, EventArgs e)
        {
            TreeNode selectedNode = this.treeView1.SelectedNode;
            if (selectedNode != null && selectedNode.Tag != null)
            {
                int typeId = Convert.ToInt32(selectedNode.Tag.ToString());
                int count = BLLFactory<DictData>.Instance.GetDictByTypeID(typeId).Count;
                if (count > 0)
                {
                    MessageUtil.ShowError("不能直接删除有项目内容的大类，请先删除其下面的项目。");
                    return;
                }

                string message = string.Format("您确定要删除节点：{0}", selectedNode.Text);
                if (MessageUtil.ShowYesNoAndWarning(message) == DialogResult.Yes)
                {
                    try
                    {
                        BLLFactory<DictData>.Instance.Delete(typeId.ToString());
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
        }

        private void menu_Refresh_Click(object sender, EventArgs e)
        {
            InitTree();
        }
    }
}
